
/**************************************************************************/
/*                                                                        */
/*  System  Name :  ICIM                                                  */
/*                                                                        */
/*  Description  :  Bis_qrs Management                                   */
/*                                                                        */
/*  MODIFICATION HISTORY                                                  */
/*    Date     Ver     Name          Description                          */
/* ---------- ----- ----------- ----------------------------------------- */
/* 2017/09/28 N0.00                Initial release                        */
/*                                                                        */
/**************************************************************************/

$(document).ready(function() {
	var inObj_Query;
    var VAL = {
        T_FBPBISPTH : "FBPBISPTH",
        T_FBPBISTOL : "FBPBISTOL",
        T_FBPBISOPE : "FBPBISOPE",
        T_FBPSWHPTH : "FBPSWHPTH",
        EVT_USR : $("#userId").text(),
        NORMAL : "0000000",
        DISABLED_ATTR : {
            "disabled" : true
        },
        ENABLED_ATTR : {
            "disabled" : false
        },
        mustSaveFlg : "N"
    };
    
	var globalBean = {
		opeObjs : {},
		getOpeDsc : function(opeID, opeVer) {
			var i, oary;
			for (i = 0; i < this.opeObjs.length; i++) {
				oary = this.opeObjs[i];
				if (typeof (opeVer) === "undefined") {
					if (opeID == oary.ope_id) {
						return oary.ope_dsc;
					}
				} else {
					if (opeID == oary.ope_id && opeVer == oary.ope_ver) {
						return oary.ope_dsc;
					}
				}
			}
		}
	};
    /**
     * grid  initialization
     */
    var iniGridInfo = function(){
    	$("#pathListGrd").jqGrid({
    		url:"",
    		datatype:"local",
    		mtype:"POST",
    		autowidth:true,
    		shrinkToFit:false,
    		scroll: true,
    		resizable : true,
    		loadonce:true,
    		pager : '#pathListPg',
    		rownumbers  :true ,//显示行号
    		viewrecords :true ,
    		colModel: [
    		    {
    		    	name: 'path_cate' , 
    		    	index: 'path_cate' , 
    		    	label: PATH_CATE_TAG , 
    		    	width: 120
    		    },{
    		    	name: 'path_id'   , 
    		    	index: 'path_id'   , 
    		    	label: PATH_ID_TAG , 
    		    	width: 180 
    		    },{
    		    	name: 'path_dsc'  , 
    		    	index: 'path_dsc'  , 
    		    	label: PATH_DSC_TAG , 
    		    	width: 220 
    		    }],
		    onSelectRow:function(id){
		    	toolFunc.onGridSelRowFnc(id);
		    },
		    beforeSelectRow :function (id) {
		    	if($("#pathDscTxt").attr("disabled") != "disabled"){
		    		showErrorDialog("", "目前是编辑状态，不可进行其它操作!");
		    		return false;
		    	}
		    	if(VAL.mustSaveFlg == 'Y'){
		    		showErrorDialog("","请先保存工艺路线站点！");
		    		return false;
		    	}
		    	return true;
		    }
    	});

      //QRS 详细子项目明细
    	$("#pathItemListGrd").jqGrid({
    		url:"",
    		datatype:"local",
    		mtype:"POST",
    		autowidth:true,
    		shrinkToFit:false,
    		scroll: true,
    		resizable : true,
    		loadonce:true,
    		rownumbers  :true ,//显示行号
    		viewrecords :true ,
    		colModel: [
    		    {
    		    	name: 'cr_ope_no',
    		    	index: 'cr_ope_no',
    		    	label: OPE_NO_TAG,
    		    	width: 150
    		    },{
    		    	name: 'cr_ope_id_fk',
    		    	index: 'cr_ope_id_fk',
    		    	label: OPE_ID_TAG ,
    		    	width: 150 
    		    },{
    		    	name: 'ope_dsc',
    		    	index: 'ope_dsc',
    		    	label: OPE_DSC_TAG,
    		    	width: 300
    		    },{
    		    	name: 'swh_avl_flg',
    		    	index: 'swh_avl_flg',
    		    	label: SWH_AVL_FLG_TAG,
    		    	width: 100 
    		    },{
    		    	name: 'skip_flg', 
    		    	index: 'skip_flg', 
    		    	label: "SKIP_FLG", 
    		    	width: 100 
    		    }],
    	    pager : '#pathItemListPg'
      });
    };
    
    /**
     * All tool functions
     * @type {Object}
     */
    var toolFunc = {
    	resizeFnc : function(){//表格自适应
            var offsetBottom, grdDivWidth,offsetBottom1, grdDivWidth1;
            //1
            grdDivWidth = $("#divLeft").width();
            offsetBottom = $(window).height() - $("#pathListDiv").offset().top-20;

            $("#pathListDiv").width(grdDivWidth*0.98);
            $("#pathListDiv").height(offsetBottom*0.99);
            $("#pathListGrd").setGridWidth(grdDivWidth*0.97);
            $("#pathListGrd").setGridHeight(offsetBottom*0.95-45);
            //2
            grdDivWidth1 = $("#divRight").width();
            offsetBottom1 = $(window).height() - $("#pathItemListDiv").offset().top-20;
            $("#pathItemListDiv").width(grdDivWidth1*0.98);
            $("#pathItemListDiv").height(offsetBottom1*0.99);
            $("#pathItemListGrd").setGridWidth(grdDivWidth1*0.97);
            $("#pathItemListGrd").setGridHeight(offsetBottom1*0.95-55);
    	},
    	initFnc : function(){
            $("input").attr(VAL.DISABLED_ATTR);
            $("select").attr(VAL.DISABLED_ATTR);
            $("input").val("");
            SelectDom.initWithSpace($("select"));
            comAddValueByDataCateFnc("#pathCateSel","RCAT","data_ext","data_desc", true);
    	},
    	iniOpeInfo : function(){
            var inTrxObj = {
                trx_id     : VAL.T_FBPBISOPE ,
                action_flg : 'L'
            };
            var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
            if(outTrxObj.rtn_code == "0000000" ) {
                var oaryA = outTrxObj.oary;
                for(i=0;i<oaryA.length;i++){
                    oaryA[i].dsc = oaryA[i].ope_id + ":" + oaryA[i].ope_dsc;
                }
                SelectDom.addSelectArr_($("#pathItemDialog_crOpeIDSel"),outTrxObj.oary,"ope_id","ope_id","ope_dsc",true);
                globalBean.opeObjs = outTrxObj.oary;
            }
    	},
    	iniSkipFlg : function(){
            $("#pathItemDialog_skipFlgSel").empty();
            $("#pathItemDialog_skipFlgSel").append("<option value = ''></option>");
            $("#pathItemDialog_skipFlgSel").append("<option value = '1'>1:自动入账(上一站出账检查)</option>");
           /* $("#pathItemDialog_skipFlgSel").append("<option value = '2'>2:出账界面检查本站未入账产品，协助发送入账请求</option>");*/
            $("#pathItemDialog_skipFlgSel").append("<option value = '2'>2:入账界面检查上一站点未出账，协助发送出账请求</option>");
            $("#pathItemDialog_skipFlgSel").append("<option value = '3'>3:跳过本站，不做制程(上一站出账检查)</option>");
/*            $("#pathItemDialog_skipFlgSel").append("<option value = '5'>5:Lv2站点，OK品跳过，不做制程(上一站出账检查)</option>");*/
            $("#pathItemDialog_skipFlgSel").select2({
    	    	theme : "bootstrap"
    	    });
    	},
        onGridSelRowFnc : function(id){
            $("#pathConditionForm  input").attr(VAL.DISABLED_ATTR);
            $("#pathConditionForm  select").attr(VAL.DISABLED_ATTR);
            var FIRST_OPE_NO ="FRST";
            var rowData = $("#pathListGrd").jqGrid("getRowData",id);
            var iaryA = {};
            var iaryB = {};
            if(rowData.path_id){
                iaryA.path_id    = rowData.path_id;
                iaryB.path_id_fk = rowData.path_id;
            }else{
            	return false;
            }
            
            iaryA.path_ver    = '00000';
            iaryB.path_ver_fk = '00000';

            //获取工艺路线信息
            var inObj = {
                trx_id     : VAL.T_FBPBISPTH,
                action_flg : "Q",
                iaryA      : [iaryA]
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if(outObj.rtn_code === VAL.NORMAL){
                oaryA = outObj.oaryA[0];
                $("#pathIDTxt").val(oaryA.path_id);
                $("#pathVerTxt").val(oaryA.path_ver);
                $("#pathDscTxt").val(oaryA.path_dsc);
                SelectDom.setSelect($("#pathCateSel"),oaryA.path_cate);
                $("#holdFlag").val(oaryA.path_stat);
            }

            //获取工艺路线item信息
            var inTrxObj ={
                trx_id     : VAL.T_FBPBISPTH,
                action_flg : "I",
                iaryB      : [iaryB],
                tbl_cnt_b  : 1
            };
            var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
            if(  outTrxObj.rtn_code == _NORMAL ) {
                $("#pathItemListGrd").jqGrid("clearGridData");
                var tbl_cnt_b = outTrxObj.tbl_cnt_b ;
                if( tbl_cnt_b > 0 ){
                    for(var i = 0; i < outTrxObj.oaryB.length; i++){
                    	outTrxObj.oaryB[i].ope_dsc = globalBean.getOpeDsc(outTrxObj.oaryB[i].cr_ope_id_fk);
                    }

                    if(tbl_cnt_b==1){
                        setGridInfo(outTrxObj.oaryB,"#pathItemListGrd");
                    }else{
                        var pv_ope_no ="";
                        for(var i=0;i<tbl_cnt_b;i++){
                            if(outTrxObj.oaryB[i].pv_ope_no==FIRST_OPE_NO){
                                $("#pathItemListGrd").jqGrid("addRowData",0,outTrxObj.oaryB[i]);
                                pv_ope_no = outTrxObj.oaryB[i].cr_ope_no;
                                break;
                            }
                        }
                        for(var i=0;i<tbl_cnt_b;i++){
                            if(outTrxObj.oaryB[i].pv_ope_no==FIRST_OPE_NO){
                                continue;
                            }else{
                                var newRowID  = getGridNewRowID($("#pathItemListGrd"));
                                for(var j=0;j<tbl_cnt_b;j++){
                                    if(outTrxObj.oaryB[j].pv_ope_no==pv_ope_no){
                                        $("#pathItemListGrd").jqGrid("addRowData",newRowID,outTrxObj.oaryB[j]);
                                        pv_ope_no = outTrxObj.oaryB[j].cr_ope_no;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        },
        savePathInfoFnc : function(){
            var actionFlg = $("#pathIDTxt").attr("disabled") === "disabled" ? "U" : "A";
            var path_id = $.trim($("#pathIDTxt").val());
            if(!path_id){
                showErrorDialog("","请输入工艺路线代码");
                $("#pathIDTxt").focus();
                return false;
            }
            
            var path_dsc = $.trim($("#pathDscTxt").val());
            if(!path_dsc){
                showErrorDialog("","请输入工艺路线描述");
                $("#pathDscTxt").focus();
                return false;
            }
            
            var pathCate = $.trim($("#pathCateSel").val());
            if(!pathCate){
                showErrorDialog("","请选择工艺路线类别");
                return false;
            }

            var rowIds = $("#pathItemListGrd").jqGrid("getDataIDs");
            if(rowIds.length == 0){
                showErrorDialog("","请添加工艺路线站点");
                return false;
            }
            var rowData = $("#pathItemListGrd").jqGrid("getRowData",rowIds[0]);
            var first_ope_no = rowData.cr_ope_no;

            var iaryA     = {
                path_id    : path_id,
                path_ver   : '00000',
                path_dsc   : path_dsc,
                path_cate  : pathCate,
                path_stat  : $.trim($("#holdFlag").val()),
                first_ope_no :first_ope_no
            };

            var inTrxObj = {
                trx_id      : VAL.T_FBPBISPTH  ,
                evt_usr     : VAL.EVT_USR ,
                action_flg  : actionFlg,
                iaryA       : [iaryA],
                tbl_cnt_a   : 1
            };
            var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
            if(  outTrxObj.rtn_code == VAL.NORMAL ) {
                VAL.mustSaveFlg = 'N';
                var newRowID = getGridNewRowID("#pathListGrd");
                if($("#pathIDTxt").attr("disabled") != "disabled"){
                    $("#pathListGrd").jqGrid("addRowData",newRowID,iaryA);
                }
                $("#pathConditionForm input").attr({'disabled':true});
                $("#pathConditionForm select").attr({'disabled':true});
            }else{
            	return false;
            }
            return true;
        },
        savePathItemFnc : function(){
            var rowIDs = $("#pathItemListGrd").jqGrid("getDataIDs");
            if(rowIDs.length<=0){
                return false;
            }
            var rowCnt = rowIDs.length;
            var iaryBList = new Array();
            var iaryA = {};
            for(var i=0;i<rowIDs.length;i++){
                var pvRowData  = null;
                var crRowData  = null;
                var nxRowData  = null;

                var crRowData =$("#pathItemListGrd").jqGrid("getRowData",rowIDs[i]);
                if(i<rowCnt-1){
                    rowData2 = $("#pathItemListGrd").jqGrid("getRowData",rowIDs[i+1]);
                }
                var iaryB = crRowData;
                iaryB.cr_ope_ver_fk = '00000';
                iaryB.path_id_fk  = $.trim($("#pathIDTxt").val());
                iaryB.path_ver_fk = '00000';
                if(i==0){
                    nxRowData       = $("#pathItemListGrd").jqGrid("getRowData",rowIDs[i+1]);
                    iaryB.pv_ope_no = "FRST" ;
                    iaryB.nx_ope_no = nxRowData.cr_ope_no ;
                    iaryA.first_ope_no = crRowData.cr_ope_no;
                }else if(i==rowCnt.length-1){
                    crRowData       = $("#pathItemListGrd").jqGrid("getRowData",rowIDs[i-1]);
                    iaryB.pv_ope_no = pvRowData.cr_ope_no ;
                    iaryB.nx_ope_no = "LAST" ;
                }else{
                    nxRowData = $("#pathItemListGrd").jqGrid("getRowData",rowIDs[i+1]);
                    crRowData = $("#pathItemListGrd").jqGrid("getRowData",rowIDs[i-1]);
                    iaryB.pv_ope_no = crRowData.cr_ope_no ;
                    iaryB.nx_ope_no = nxRowData.cr_ope_no ;
                }
                iaryBList.push(iaryB);
            }
            var inTrxObj = {
                trx_id      : VAL.T_FBPBISPTH ,
                action_flg  : 'N'        ,
                iaryB       : iaryBList  ,
                evt_usr     : VAL.EVT_USR,
                tbl_cnt_b   : rowCnt     ,
                iaryA       : [iaryA]      ,
                tbl_cnt_a   : 1
            };
            var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
            if(  outTrxObj.rtn_code == VAL.NORMAL ) {
                VAL.mustSaveFlg ='N';
            }else{
            	return false;
            }
            return true;
        },
        showAddPathItemDialog : function( pathItemObj ){
            function pathItemDialogShowFnc(pathItemObj){
                $('#pathItemDialog  input').attr(VAL.ENABLED_ATTR);
                $('#pathItemDialog  input').val("");
                $('#pathItemDialog  select').attr(VAL.ENABLED_ATTR);
                SelectDom.setSelect($('#pathItemDialog  select'), "");
                $('#pathItemDialog  checkbox').attr(VAL.ENABLED_ATTR);
                $('#pathItemDialog  checkbox').attr("checked",true);

                if(pathItemObj.action_flg=="N"){
                    $("#pathItemDialog_savePathItemBtn").data("save_flg","N");
                }else{
                    $('#pathItemDialog_crOpeNOTxt').attr(VAL.DISABLED_ATTR);
                    $("#pathItemDialog_savePathItemBtn").data("save_flg","R");

                    $("#pathItemDialog_crOpeNOTxt").val(pathItemObj.cr_ope_no);
                    SelectDom.setSelect($("#pathItemDialog_crOpeIDSel"),pathItemObj.cr_ope_id_fk);
                    SelectDom.setSelect($("#pathItemDialog_skipFlgSel"),pathItemObj.skip_flg);

                    if(pathItemObj.swh_avl_flg=="Y"){
                        $("#pathItemDialog_swhAvlFlgChk").attr("checked","true");
                    }else{
                        $("#pathItemDialog_swhAvlFlgChk").removeAttr("checked");
                    }
                }
            }
            function contains(arr,obj){
                var i= arr.length;
                while (i--) {
                    if (arr[i].cr_ope_no === obj) {
                        return true;
                    }
                }
                return false;
            }

            function savePathItemFunc(){
                var skip_flg = $("#pathItemDialog_skipFlgSel").val();
                var action_flg = $("#pathItemDialog_savePathItemBtn").data("save_flg");
                if(!action_flg){
                    return false;
                }
                
                var cr_ope_no = $.trim($('#pathItemDialog_crOpeNOTxt').val());
                if(!cr_ope_no){
                    showErrorDialog("","请输入站点序号");
                    return false;
                }

                var cr_ope_id = $.trim($("#pathItemDialog_crOpeIDSel").val());
                if(!cr_ope_id){
                    showErrorDialog("","请选择站点代码");
                    return false;
                }
                
                var rows = $("#pathItemListGrd").jqGrid('getRowData');

                if (rows.length>0 && contains(rows,$('#pathItemDialog_crOpeNOTxt').val()) && !$('#pathItemDialog_crOpeNOTxt').attr("disabled")){
                    showErrorDialog("","不能重复添加！");
                    return;
                }
                var iaryB = {
                    path_id_fk    : $.trim($("#pathIDTxt").val()),
                    path_ver_fk   : '00000',
                    cr_ope_no     : cr_ope_no,
                    cr_ope_id_fk  : cr_ope_id,
                    cr_ope_ver_fk : '00000',
                    ope_dsc : $("#pathItemDialog_crOpeIDSel").find("option:selected").text().split(":")[1],
                    skip_flg:skip_flg

                };
                if($("#pathItemDialog_swhAvlFlgChk").attr("checked")=="checked"){
                    iaryB.swh_avl_flg="Y";
                }else{
                    iaryB.swh_avl_flg="N";
                }
                
                $('#pathItemDialog').modal("hide");
                
                var registActionFlg = $("#save_btn").data("regist_flg");
                if(action_flg=="N"){
                    var newRowID = getGridNewRowID("#pathItemListGrd");
                    $("#pathItemListGrd").jqGrid("addRowData",newRowID,iaryB);
                    if(registActionFlg=="A"||registActionFlg=="AB"){
                        $("#save_btn").data("regist_flg","AB");
                    }else{
                        $("#save_btn").data("regist_flg","B");
                    }

                }else{
                    var selectRowID = $("#pathItemListGrd").jqGrid("getGridParam","selrow");
                    $("#pathItemListGrd").jqGrid("setRowData",selectRowID,iaryB);
                    if(registActionFlg=="A"){
                        $("#save_btn").data("regist_flg","AB");
                    }else{
                        $("#save_btn").data("regist_flg","B");
                    }
                }
    	        VAL.mustSaveFlg = 'Y';
            }
            $('#pathItemDialog').modal({
                backdrop:true ,
                keyboard:false,
                show:true
            });
            $('#pathItemDialog').unbind('shown.bs.modal');
            $("#pathItemDialog_savePathItemBtn").unbind('click');
            $('#pathItemDialog').bind('shown.bs.modal',pathItemDialogShowFnc(pathItemObj));
            $("#pathItemDialog_savePathItemBtn").bind('click',savePathItemFunc);
        }
    };
    
    var baseFnc = {
        com_get_1st_inf : function(){
            var rowIds = $("#pathListGrd").jqGrid('getDataIDs');
            if(rowIds.length>0){
                $("#pathListGrd").setSelection(rowIds[0], true);
            }
        }
    };
    
    /**
     * All button click function
     * @type {Object}
     */
    var btnFunc = {
		diaLogQueryFnc : function(){
            var iaryA = {};
            var path_cate = $.trim($("#queryPathDialog_pathCateTxt").val());
            if(path_cate){
                iaryA.path_cate = path_cate;
            }
            var path_id = $.trim($("#queryPathDialog_pathIDTxt").val());
            if(path_id){
                iaryA.path_id = path_id;
            }
            inObj_Query ={
                trx_id     : VAL.T_FBPBISPTH,
                action_flg : 'Q'        ,
                iaryA      : [iaryA]      ,
                tbl_cnt_a  : 1
            };
            var  outTrxObj = comTrxSubSendPostJson(inObj_Query);
            if(  outTrxObj.rtn_code == VAL.NORMAL ) {
                setGridInfo(outTrxObj.oaryA,"#pathListGrd");
                $('#queryPathDialog').modal("hide");
                baseFnc.com_get_1st_inf();
            }
		},
		queryFnc : function(){
			if($("#pathDscTxt").attr("disabled") != "disabled"){
	        	showErrorDialog("", "目前是编辑状态，不可进行其它操作!");
	            return false;
	        }
	        if(VAL.mustSaveFlg == 'Y'){
	            showErrorDialog("","请先保存工艺路线站点！");
	            return false;
	        }

	        $('#queryPathDialog').modal({
	            backdrop:true,
	            keyboard:false,
	            show:false
	        });
	        
	        $('#queryPathDialog').unbind('shown.bs.modal');
	        $("#queryPathDialog_queryBtn").unbind('click');

	        $('#queryPathDialog').bind('shown.bs.modal');
	        $('#queryPathDialog').modal("show");
	        comAddValueByDataCateFnc("#queryPathDialog_pathCateTxt","RCAT","data_ext","data_desc", true);
	        $("#queryPathDialog_queryBtn").bind('click',btnFunc.diaLogQueryFnc);
	        $("#queryPathDialog input").attr(VAL.ENABLED_ATTR);
	        $("#queryPathDialog input").val("");
	        $("#queryPathDialog select").attr(VAL.ENABLED_ATTR);
		},
		addFnc : function(){
	        if($("#pathDscTxt").attr("disabled") != "disabled"){
	            showErrorDialog("", "目前是编辑状态，不可进行其它操作!");
	            return false;
	        }
	        if(VAL.mustSaveFlg == 'Y'){
	            showErrorDialog("","请先保存工艺路线站点！");
	            return false;
	        }

	        $("#pathConditionForm input").attr(VAL.ENABLED_ATTR);
	        $("#pathConditionForm select").attr(VAL.ENABLED_ATTR);
	        $("#holdFlag").attr(VAL.DISABLED_ATTR);
	        $("#pathConditionForm input").val("");
	        SelectDom.setSelect($("select"), "", "");
	        $("#pathItemListGrd").jqGrid("clearGridData");
	        $("#save_btn").data("regist_flg","A");
	        $("#holdFlag").val("HOLD");
	        $("#pathIDTxt").focus();
		},
		updateFnc : function(){
	        var selRowId = $("#pathListGrd").jqGrid("getGridParam","selrow");
	        if (!selRowId) {
	            showErrorDialog("", "请选择需要修改的工艺路线信息");
	            return false;
	        }
	        $("#pathConditionForm input:not(#pathIDTxt,#pathVerTxt)").attr(VAL.ENABLED_ATTR);
	        $("#pathConditionForm select").attr(VAL.ENABLED_ATTR);
	        $("#holdFlag").attr(VAL.DISABLED_ATTR);
	        $("#pathDscTxt").focus();
		},
		deleteFnc : function(){
	       var selectID = $("#pathListGrd").jqGrid("getGridParam", "selrow" );
	        if(!selectID){
	            showErrorDialog("","请选择需要删除的工艺路线");
	            return false;
	        }
	        if($("#pathDscTxt").attr("disabled") != "disabled"){
	        	showErrorDialog("", "目前是编辑状态，不可进行其它操作!");
	            return false;
	        }
	        if(VAL.mustSaveFlg == 'Y'){
	            showErrorDialog("","请先保存工艺路线站点！");
	            return false;
	        }
	        var rowData = $("#pathListGrd").jqGrid("getRowData",selectID);
	        $("#delete_btn").showCallBackWarnningDialog({
	            errMsg  : "是否删除工艺路线,请确认!!!!",
	            callbackFn : function(data) {
	                if(data.result==true){
	                    var iaryA ={};
	                    iaryA.path_id  = rowData.path_id;
	                    iaryA.path_ver = '00000';
	                    var inTrxObj = {
	                        trx_id     : VAL.T_FBPBISPTH,
	                        evt_usr    : VAL.EVT_USR,
	                        action_flg : "D",
	                        iaryA      : [iaryA],
	                        tbl_cnt_a  : 1
	                    };
	                    var outTrxObj = comTrxSubSendPostJson(inTrxObj);
	                    if(  outTrxObj.rtn_code == _NORMAL ) {
	                        showSuccessDialog("删除工艺路线成功");
	                        SelectDom.setSelect($("select"), "", "");
	                        $("#pathConditionForm input").val("");
	                        $("#pathListGrd").jqGrid('delRowData', selectID);
	                        $("#pathItemListGrd").jqGrid("clearGridData");
	                        baseFnc.com_get_1st_inf();
	                    }
	                }
	            }
	        });
		},
		copyFnc : function(){
	        if($("#pathDscTxt").attr("disabled") != "disabled"){
	        	showErrorDialog("", "目前是编辑状态，不可进行其它操作!");
	            return false;
	        }
	        if(VAL.mustSaveFlg == 'Y'){
	            showErrorDialog("","请先保存工艺路线站点！");
	            return false;
	        }
	        var selRowId = $("#pathListGrd").jqGrid("getGridParam","selrow");
	        if (!selRowId) {
	            showErrorDialog("", "请选择需要复制的工艺路线信息");
	            return false;
	        }
	        $("#pathConditionForm input").attr(VAL.ENABLED_ATTR);
	        $("#pathConditionForm select").attr(VAL.ENABLED_ATTR);
	        $("#holdFlag").val("HOLD");
	        $("#holdFlag").attr(VAL.DISABLED_ATTR);
	        $("#pathIDTxt").focus();
		},
		registFnc : function(){
	        if($("#pathIDTxt").attr("disabled") !== "disabled"){
	            if(toolFunc.savePathInfoFnc()){
	                showSuccessDialog("新增工艺路线成功");
	                toolFunc.savePathItemFnc();
	            }
	        }else if($("#pathIDTxt").attr("disabled") === "disabled"){
	            if(toolFunc.savePathInfoFnc() && toolFunc.savePathItemFnc()){
	                showSuccessDialog("修改工艺成功");
	                var id =$("#pathListGrd").jqGrid('getGridParam','selrow');
	                toolFunc.onGridSelRowFnc(id);
	            }
	        }else{
	            if(toolFunc.savePathItemFnc()){
	                showSuccessDialog("新增工艺路线站点成功");
	            }
	        }
		},
		rollbackFnc : function(){
			if($("#pathDscTxt").attr("disabled") == "disabled"){
				showErrorDialog("", "没有编辑，无需撤回");
				return false;
			}
	        VAL.mustSaveFlg = 'N';
	        $("#pathConditionForm input").attr(VAL.DISABLED_ATTR);
	        $("#pathConditionForm select").attr(VAL.DISABLED_ATTR);
	        var outObj = comTrxSubSendPostJson(inObj_Query);
	        if (outObj.rtn_code === VAL.NORMAL) {
	        	setGridInfo(outObj.oaryA,"#pathListGrd");
	            baseFnc.com_get_1st_inf();
	        }
		},
		clearFnc : function(){
	        $("input").val("");
	        SelectDom.setSelect($("select"), "", "");
	        $("#pathConditionForm input").attr(VAL.DISABLED_ATTR);
	        $("#pathConditionForm select").attr(VAL.DISABLED_ATTR);
	        $("#pathListGrd").jqGrid("clearGridData");
	        $("#pathItemListGrd").jqGrid("clearGridData");
		},
		okFnc : function(){
	        if($("#pathDscTxt").attr("disabled") != "disabled"){
	        	showErrorDialog("", "目前是编辑状态，不可进行其它操作!");
	            return false;
	        }
	        var selRowId = $("#pathListGrd").jqGrid("getGridParam","selrow");
	        if (!selRowId) {
	            showErrorDialog("", "请选择需要修改的工艺路线信息");
	            return false;
	        }
	        if(VAL.mustSaveFlg == 'Y'){
	            showErrorDialog("","请先保存工艺路线站点！");
	            return false;
	        }
	        var iaryA     = {
	            path_id    : $.trim($("#pathIDTxt").val()),
	            path_ver   : '00000',
	            path_stat  : 'OK'
	        };
	        var inTrxObj = {
	            trx_id      : VAL.T_FBPBISPTH  ,
	            evt_usr     : VAL.EVT_USR ,
	            action_flg  : 'O',
	            iaryA       : [iaryA]      ,
	            tbl_cnt_a   : 1
	        };
	        var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
	        if(  outTrxObj.rtn_code == VAL.NORMAL ) {
	            showSuccessDialog("修改成功");
	            $("#holdFlag").val("OK");
	        }else{
	            return false;
	        }
            return true;
		},
		holdFnc : function(){
	        if($("#pathDscTxt").attr("disabled") != "disabled"){
	        	showErrorDialog("", "目前是编辑状态，不可进行其它操作!");
	            return false;
	        }
	        var selRowId = $("#pathListGrd").jqGrid("getGridParam", "selrow");
	        if (!selRowId) {
	            showErrorDialog("", "请选择需要修改的工艺路线信息");
	            return false;
	        }
	        if(VAL.mustSaveFlg == 'Y'){
	            showErrorDialog("","请先保存工艺路线站点！");
	            return false;
	        }
	        var iaryA = {
	            path_id: $.trim($("#pathIDTxt").val()),
	            path_ver: '00000',
	            path_stat: 'HOLD'
	        };
	        var inTrxObj = {
	            trx_id: VAL.T_FBPBISPTH,
	            evt_usr: VAL.EVT_USR,
	            action_flg: 'O',
	            iaryA: [iaryA],
	            tbl_cnt_a: 1
	        };
	        var outTrxObj = comTrxSubSendPostJson(inTrxObj);
	        if (outTrxObj.rtn_code == VAL.NORMAL) {
	            showSuccessDialog("修改成功");
	            $("#holdFlag").val("HOLD");
	        }else{
	        	return false;
	        }
            return true;
		},
		addPathItemFnc : function(){
	        if($.trim($("#pathIDTxt").val())){
	            var pathItemObj  = {
	                action_flg : "N"
	            };
	            toolFunc.showAddPathItemDialog(pathItemObj);
	        }else{
	            showErrorDialog("","请在列表中选择工艺路线");
	            return false;
	        }
		},
		updPamItemFnc : function(){
	        if($.trim($("#pathIDTxt").val())){
	            var selectID = $("#pathItemListGrd").jqGrid("getGridParam","selrow");
	            if(!selectID){
	                showErrorDialog("","请选择需要修改的站点信息!");
	                return false;
	            }
	            var rowData = $("#pathItemListGrd").jqGrid("getRowData",selectID);
	            rowData.action_flg ="R";
	            toolFunc.showAddPathItemDialog(rowData);
	        }
	    },
	    delPamItemFnc : function(){
	        var selectID = $("#pathItemListGrd").jqGrid("getGridParam","selrow");
	        if(!selectID){
	            showErrorDialog("","请选择需要删除的工艺项目");
	            return false;
	        }
	        $("#del_pathItem_btn").showCallBackWarnningDialog({
	            errMsg  : "是否删除工艺路线站点,请确认!!!!",
	            callbackFn : function(data) {
	                if(data.result==true){
	                    $("#pathItemListGrd").jqGrid("delRowData",selectID);
	                    $("#save_btn").data("regist_flg","B");
	                    $("#pathItemDialog_savePathItemBtn").data("save_flg","N");
	        	        VAL.mustSaveFlg = 'Y';
	                }
	            }
	        });
	    }
    };
    
    var BranchPath = {
    	iniOutOpeNoFnc : function(){
    		SelectDom.initWithSpace($("#out_ope_no_fk"));
	        var fr_path_id_fk = $.trim($("#fr_path_id_fk").val());
	        var fr_path_ver_fk = "00000";
	        var iary = {
	            path_id_fk : fr_path_id_fk,
	            path_ver_fk : fr_path_ver_fk
	        };
	        var inObj = {
	            trx_id : VAL.T_FBPBISPTH,
	            action_flg : 'I',
	            iaryB : [iary]
	        };
	        var outObj = comTrxSubSendPostJson(inObj);
	        if(outObj.rtn_code === VAL.NORMAL){
	             var newOaryList = new Array();
	             for(var i = 0; i < outObj.oaryB.length ; i++){
	                 newOaryList.push({
	                     ope_no : outObj.oaryB[i].cr_ope_no,
	                     ope_id : outObj.oaryB[i].cr_ope_id_fk,
	                     ope_dsc : globalBean.getOpeDsc(outObj.oaryB[i].cr_ope_id_fk)
	                 })

	             }
	             SelectDom.addSelectArr_($("#out_ope_no_fk"),newOaryList,"ope_no","ope_id","ope_dsc",true);
	        }
	    },
        showBranchPathDialogFnc : function(){
            //初始化
            $("#fr_path_id_fk").val("");
            SelectDom.initWithSpace($("#fr_ope_no_fk"));
            $("#swh_id").val("");
            $("#swh_dsc").val("");
            SelectDom.initWithSpace($("#swh_path_id_fk"));
            SelectDom.initWithSpace($("#out_ope_no_fk"));
            $(".pathInput input").attr(VAL.DISABLED_ATTR);
            $(".pathInput select").attr(VAL.DISABLED_ATTR);

            var selectRowID = $("#pathItemListGrd").jqGrid("getGridParam","selrow");
            if(!selectRowID){
                showErrorDialog("","请在工艺路线站点信息中选择需要添加分支的途程和站点");
                return false;
            }

            var rowData = $("#pathItemListGrd").jqGrid("getRowData", selectRowID);
            $("#fr_path_id_fk").val($("#pathIDTxt").val());

            SelectDom.setSelect($("#fr_ope_no_fk"), rowData.cr_ope_no, rowData.cr_ope_id_fk+":"+ globalBean.getOpeDsc(rowData.cr_ope_id_fk));

            $("#swh_path_id_fk").unbind('change');
            $("#branchPathDialog_add_branchItem_btn").unbind('click');
            $("#branchPathDialog_del_branchItem_btn").unbind('click');
            $("#branchPathDialog_branch_regist_btn").unbind('click');
            $("#swh_path_id_fk").bind('change',BranchPath.swhpthIdChangeFnc);
            $("#branchPathDialog_add_branchItem_btn").bind('click',BranchPath.addBranchFnc);
            $("#branchPathDialog_del_branchItem_btn").bind('click',BranchPath.deleteBranchFnc);
            $("#branchPathDialog_branch_regist_btn").bind('click',BranchPath.saveBranchFnc);

            $("#refreshGrid").unbind('click');
            $("#refreshGrid").bind('click',BranchPath.queryBranchByPath);
            BranchPath.initialBranchPathDialogFnc();
            BranchPath.getBranchByPath( $("#fr_path_id_fk").val(), $("#fr_path_ver_fk").val(),$("#fr_ope_no_fk").find("option:selected").val());

            $('#branchPathDialog').modal({
                backdrop:true ,
                keyboard:false,
                show:false
            });
            $('#branchPathDialog').unbind('shown.bs.modal');
            $('#branchPathDialog').bind('shown.bs.modal',BranchPath.iniBranchSelFnc);
            $('#branchPathDialog').modal("show");
        },
        getBranchByPath : function(fr_path_id_fk,fr_path_ver_fk,fr_ope_no_fk){
            var pathItemRowData,
                iary,
                inTrxObj,
                outTrxObj;
            iary = {
                fr_path_id_fk  : fr_path_id_fk,
                fr_path_ver_fk : '00000',
                fr_ope_no_fk   : fr_ope_no_fk
            };
            inTrxObj ={
                trx_id      : VAL.T_FBPSWHPTH ,
                action_flg  : "Q",
                iary        : [iary],
                tbl_cnt     : 1
            };
            outTrxObj = comTrxSubSendPostJson(inTrxObj);
            if(outTrxObj.rtn_code==VAL.NORMAL){
                for(var i = 0; i < outTrxObj.oary.length; i++){
                    var path_id = outTrxObj.oary[i].out_path_id_fk;
                    var path_ver = '00000';
                    var ope_no = outTrxObj.oary[i].out_ope_no_fk;
                    var iary1 = {
                        path_id_fk : path_id,
                        path_ver_fk : path_ver,
                        cr_ope_no : ope_no
                    };

                    var inObj1 = {
                        trx_id      : VAL.T_FBPBISPTH ,
                        action_flg  : "I",
                        iaryB        : [iary1]
                    }
                    var outObj1 = comTrxSubSendPostJson(inObj1);
                    if(outObj1.rtn_code==VAL.NORMAL){
                        var cr_ope_id_fk = outObj1.oaryB[0].cr_ope_id_fk;

                        var iary2 = {
                            ope_id : cr_ope_id_fk,
                            ope_ver : '00000'
                        }
                        var inObj2 = {
                            trx_id      : VAL.T_FBPBISOPE ,
                            action_flg  : "Q",
                            iary        : [iary2]
                        }
                        var outObj2 = comTrxSubSendPostJson(inObj2);
                        if(outObj2.rtn_code==VAL.NORMAL){
                            var ope_dsc = outObj2.oary[0].ope_dsc;
                            outTrxObj.oary[i].out_ope_no_fk = cr_ope_id_fk +":" + ope_dsc;
                        }
                    }
                }
                setGridInfo(outTrxObj.oary,"#branchPathListGrd");
            }
        },
        initialBranchPathDialogFnc : function(){
            var branchItemInfoCM = [
                {name: 'swh_id'            , index: 'swh_id'           , label: SWITCH_ID_TAG       ,editable:true, width: 80 },
                {name: 'swh_path_id_fk'    , index: 'swh_path_id_fk'   , label: SWH_PATH_ID_FK_TAG  ,editable:true, width: 120 },
                {name: 'swh_dsc'           , index: 'swh_dsc'          , label: SWH_DSC_TAG         ,editable:true, width: 200 },
                {name: 'out_ope_no_fk'     , index: 'out_ope_no_fk'    , label: OUT_OPE_NO_FK_TAG   ,editable:true, width: 110 }
            ];
            $("#branchPathListGrd").jqGrid({
                url:"",
                datatype:"json",
                mtype:"POST",
                width: 500,
                shrinkToFit:true,
                scroll: true,
                loadonce:false,
                viewrecords : true, //显示总记录数
                rownumbers  :true ,//显示行号
                emptyrecords :true ,
                forceFit:true ,
                pager  : '#branchPathListPg',
                colModel: branchItemInfoCM
            });
        },
        saveBranchFnc : function(){
            var iary,inTrxObj,outTrxObj;
            var swh_id = $.trim($("#swh_id").val());
            var swh_path_id_fk = $.trim($("#swh_path_id_fk").val());
            var out_ope_no_fk = $.trim($("#out_ope_no_fk").val());
            if (!swh_id) {
                showErrorDialog("","请输入分支号!");
                return false;
            }
            if (!swh_path_id_fk) {
                showErrorDialog("","请选择分支工艺!");
                return false;
            }
            if (!out_ope_no_fk) {
                showErrorDialog("","请输入转出站点!");
                return false;
            }

            iary = {
                swh_id          : swh_id          ,
                swh_dsc         : $.trim($("#swh_dsc").val()),
                swh_path_id_fk  : swh_path_id_fk  ,
                swh_path_ver_fk : '00000' ,
                out_ope_no_fk   : out_ope_no_fk   ,
                fr_path_id_fk   : $("#fr_path_id_fk").val()   ,
                fr_path_ver_fk  : '00000' ,
                fr_ope_no_fk    : $("#fr_ope_no_fk").find("option:selected").val()    ,
                swh_typ         : "R"
            };

            inTrxObj ={
                trx_id      : VAL.T_FBPSWHPTH ,
                action_flg  : "A",
                evt_usr     : VAL.EVT_USR,
                iary        : [iary]
            };
            
            outTrxObj = comTrxSubSendPostJson(inTrxObj);
            if(outTrxObj.rtn_code==VAL.NORMAL){
            	BranchPath.queryBranchByPath();
                $(".pathInput input").val("");
                SelectDom.setSelect($("#swh_path_id_fk"), "", "");
                SelectDom.setSelect($("#out_ope_no_fk"), "", "");
            }
        },
        deleteBranchFnc : function(){
            var selectRowID,rowData,iary,inTrx,outTrx;
            var selectRowID = $("#branchPathListGrd").jqGrid("getGridParam","selrow");
            if (!selectRowID) {
            	showErrorDialog("","请选择需要删除的分支工艺路线设定!");
                return false;
            }
            var rowData = $("#branchPathListGrd").jqGrid("getRowData",selectRowID);

            iary = {
                fr_path_id_fk   : $.trim($("#fr_path_id_fk").val()),
                fr_path_ver_fk  : '00000'  ,
                fr_ope_no_fk    : $.trim($("#fr_ope_no_fk").val()),
                swh_id          : rowData['swh_id'],
                swh_typ         : "R"
            };

            inTrx = {
                trx_id      : VAL.T_FBPSWHPTH ,
                action_flg  : "D",
                evt_usr     : VAL.EVT_USR,
                iary        : [iary]
            };

            outTrx = comTrxSubSendPostJson(inTrx);
            if(outTrx.rtn_code==VAL.NORMAL){
            	BranchPath.queryBranchByPath();
                showSuccessDialog("删除成功!");
            }

        },
        addBranchFnc : function(){
            $(".pathInput input[id!='swh_dsc']").attr(VAL.ENABLED_ATTR);
            $(".pathInput select").attr(VAL.ENABLED_ATTR);
            BranchPath.iniOutOpeNoFnc();
            $("#swh_id").focus();
        },
        iniBranchSelFnc : function(){
            var inTrxObj = {
                trx_id     : VAL.T_FBPBISPTH ,
                action_flg : "G"
            };
            var outTrxObj = comTrxSubSendPostJson(inTrxObj);
            SelectDom .addSelectArr($("#swh_path_id_fk"), outTrxObj.oaryA, "path_id","path_id","",true);
        },
        swhpthIdChangeFnc : function(){
            var swh_path_id = $.trim($("#swh_path_id_fk").val());
            if(!swh_path_id){
            	$("#swh_dsc").val("");
            	return false;
            }
            var iaryA ={
                path_id : swh_path_id
            };
            var inTrxObj={
                trx_id      : VAL.T_FBPBISPTH ,
                action_flg  : "Q"        ,
                iaryA        : [iaryA]
            };
            var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
            $("#swh_dsc").val(outTrxObj.oaryA[0].path_dsc);
        },

        queryBranchByPath : function(){
            var iary,
                inTrxObj,
                outTrxObj;
            iary = {
                fr_path_id_fk  : $.trim($("#fr_path_id_fk").val()),
                fr_path_ver_fk : '00000' ,
                fr_ope_no_fk   : $.trim($("#fr_ope_no_fk").val())
            };
            inTrxObj ={
                trx_id      : VAL.T_FBPSWHPTH ,
                action_flg  : "Q",
                iary        : [iary],
                tbl_cnt     : 1
            };
            outTrxObj = comTrxSubSendPostJson(inTrxObj);
            if(outTrxObj.rtn_code==VAL.NORMAL){
                for(var i = 0; i < outTrxObj.oary.length; i++){
                    var path_id = outTrxObj.oary[i].out_path_id_fk;
                    var path_ver = '00000';
                    var ope_no = outTrxObj.oary[i].out_ope_no_fk;
                    var iary1 = {
                        path_id_fk : path_id,
                        path_ver_fk : path_ver,
                        cr_ope_no : ope_no
                    };

                    var inObj1 = {
                        trx_id      : VAL.T_FBPBISPTH ,
                        action_flg  : "I",
                        iaryB        : [iary1]
                    }
                    var outObj1 = comTrxSubSendPostJson(inObj1);
                    if(outObj1.rtn_code==VAL.NORMAL){
                        var cr_ope_id_fk = outObj1.oaryB[0].cr_ope_id_fk;

                        var iary2 = {
                            ope_id : cr_ope_id_fk,
                            ope_ver : '00000'
                        }
                        var inObj2 = {
                            trx_id      : VAL.T_FBPBISOPE ,
                            action_flg  : "Q",
                            iary        : [iary2]
                        }
                        var outObj2 = comTrxSubSendPostJson(inObj2);
                        if(outObj2.rtn_code==VAL.NORMAL){
                            var ope_dsc = outObj2.oary[0].ope_dsc;
                            outTrxObj.oary[i].out_ope_no_fk = cr_ope_id_fk +":" + ope_dsc;
                        }
                    }
                }
                setGridInfo(outTrxObj.oary,"#branchPathListGrd");
            }
        }
    }
    
    /**
     * Bind button click action
     */
    var iniButtonAction = function(){
    	$("#query_btn").click(function(){
    		btnFunc.queryFnc();
    	});
    	
        $("#add_btn").click(function(){
        	btnFunc.addFnc();
        });
        
        $("#update_btn").click(function(){
        	btnFunc.updateFnc();
        });
        
        $("#delete_btn").click(function(){
        	btnFunc.deleteFnc();
        });
        
        $("#copy_btn").click(function(){
        	btnFunc.copyFnc();
        });
        
        $("#save_btn").click(function(){
        	btnFunc.registFnc();
        });
        
        $("#rollback_btn").click(function(){
        	btnFunc.rollbackFnc();
        });
        
        $("#clear_btn").click(function(){
        	btnFunc.clearFnc();
        });
        
        $("#ok_btn").click(function(){
            btnFunc.okFnc();
        });
        $("#hold_btn").click(function(){
            btnFunc.holdFnc();
        });
        $("#add_pathItem_btn").click(function(){
        	btnFunc.addPathItemFnc();
        });
        
        $("#upd_pathItem_btn").click(function(){
        	btnFunc.updPamItemFnc();
        });
        
        $("#del_pathItem_btn").click(function(){
        	btnFunc.delPamItemFnc();
        });
        
        $("#branch_pathItem_btn").click(function (){
            var selectRowID = $("#pathItemListGrd").jqGrid("getGridParam","selrow");
            if(!selectRowID){
                showErrorDialog("","请在工艺路线信息中选择需要添加分支的途程和站点");
                return false;
            }
            BranchPath.showBranchPathDialogFnc();
        });
    };
    
    /**
     * Ini contorl's data
     */
    var iniContorlData = function(){
    	toolFunc.initFnc();
    	toolFunc.iniOpeInfo();
    	toolFunc.iniSkipFlg();
    };
    
    var otherActionBind = function(){
        //Stop from auto commit
        $("form").submit(function(){
            return false;
        });

        $(window).resize(function() {
        	toolFunc.resizeFnc();
        });
    };
    
    /**
     * Ini view, data and action bind
     */
    var initializationFunc = function(){
    	iniGridInfo();
    	toolFunc.resizeFnc();
        iniButtonAction();
        otherActionBind();
        iniContorlData();
    };
    
    initializationFunc();
    //enter
    /*document.onkeydown=function(event){
        var e = event || window.event || arguments.callee.caller.arguments[0];
        if(e && e.keyCode==13){ // 按 enter
            $('.bootbox').modal("hide");
        }
    }*/
});